package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Vector;

public class OldBook extends JInternalFrame {
    JTable bookTable;
    JScrollPane tableScrollPane;
    DefaultTableModel tableModel;
    JTextField xiajiaText = new JTextField(20);

    public OldBook() {
        super("下架或上架图书");
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        getContentPane().setLayout(new BorderLayout());  //设置布局
        setBounds(10, 10, 1550, 600);
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭

        // 建立表格
        Vector header1, columns1;
        header1 = Dao.bookOldInfoHeader(false);
        columns1 = Dao.bookOldInfoColumns();
        tableModel = new DefaultTableModel(columns1, header1);
        //绘制表格
        bookTable = new JTable(tableModel);
        bookTable.setAutoCreateRowSorter(true);//设置点击表头可排序
        tableScrollPane = new JScrollPane(bookTable);
        bookTable.setRowHeight(25);//设置行高
        bookTable.setFont(new Font("宋体", Font.PLAIN, 20));//行字体大小
        bookTable.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 20));//表头字体大小
        FitTableColumns(bookTable);// 表格自适应列宽
        DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
        thr.setHorizontalAlignment(JLabel.CENTER);
        bookTable.getTableHeader().setDefaultRenderer(thr);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        bookTable.setDefaultRenderer(Object.class, tcr);

        //把表格滚动面板加入主面板
        getContentPane().add(tableScrollPane, BorderLayout.NORTH);

        JPanel xiajiaPanel = new JPanel(new FlowLayout());
        final JLabel ISBNLabel = new JLabel("ISBN:");
        ISBNLabel.setFont(new Font("宋体", Font.PLAIN, 20));
        xiajiaText.setFont(new Font("宋体", Font.PLAIN, 20));
        xiajiaPanel.add(ISBNLabel);
        xiajiaPanel.add(xiajiaText);

        JButton submit = new JButton("下架");
        JButton reset = new JButton("上架");
        submit.setFont(new Font("宋体", Font.PLAIN, 20));
        reset.setFont(new Font("宋体", Font.PLAIN, 20));
        submit.addActionListener(new submitAction());
        reset.addActionListener(new resetAction());
        xiajiaPanel.add(submit);
        xiajiaPanel.add(reset);

        getContentPane().add(xiajiaPanel, BorderLayout.CENTER);

    }

    public void FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier()
                            , false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
                        myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }

    private class submitAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (xiajiaText.getText().isEmpty()) {
                JOptionPane.showMessageDialog(OldBook.this, "请输入内容");
                return;
            } else {
                if (Dao.xiaJia(xiajiaText.getText())) {
                    JOptionPane.showMessageDialog(OldBook.this, "已下架");
                    Vector header1, columns1;
                    header1 = Dao.bookOldInfoHeader(false);
                    columns1 = Dao.bookOldInfoColumns();
                    tableModel.setDataVector(columns1, header1);
                } else {
                    JOptionPane.showMessageDialog(OldBook.this, "下架失败");
                }
            }
        }
    }

    private class resetAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (xiajiaText.getText().isEmpty()) {
                JOptionPane.showMessageDialog(OldBook.this, "请输入内容");
                return;
            } else {
                if (Dao.shangJia(xiajiaText.getText())) {
                    JOptionPane.showMessageDialog(OldBook.this, "已上架");
                    Vector header1, columns1;
                    header1 = Dao.bookOldInfoHeader(false);
                    columns1 = Dao.bookOldInfoColumns();
                    tableModel.setDataVector(columns1, header1);
                } else {
                    JOptionPane.showMessageDialog(OldBook.this, "下架失败");
                }
            }
        }
    }
}
